package com.thingsgrid.device.service.impl;

import com.thingsgrid.device.base.AbstractDevice;
import com.thingsgrid.device.dto.DeviceMaintenanceRecordDTO;
import com.thingsgrid.device.entity.Device;
import com.thingsgrid.device.entity.Product;
import com.thingsgrid.device.entity.ProductType;
import com.thingsgrid.device.mapper.DeviceMaintenanceMapper;
import com.thingsgrid.device.service.DeviceMaintenanceService;
import com.thingsgrid.mqtthelper.feign.MqttHelper;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 功能说明：设备维护实现类
 *
 * @author 李龙飞
 * @since 2019/9/4 15:45
 */
@Service
@AllArgsConstructor
@ComponentScan("com.device.mqtthelper.feign")
public class DeviceMaintenanceServiceImpl extends AbstractDevice implements DeviceMaintenanceService {

	private MqttHelper mqttHelper;

	@Autowired
	private DeviceMaintenanceMapper deviceMaintenanceMapper;


	/**
	 * 查询设备列表
	 * @param productType 所属产品
	 * @param productId 产品id
	 * @param deviceId 设备Id
	 * @param currentPage 当前页码
	 * @param pageSize 总页数
	 * @author 李龙飞
	 * @since 2019/9/4 15:45
	 * @return List<Device>
	 */
	@Override
	public Object getDeviceList(String productType, String productId, String deviceId, Integer currentPage, Integer pageSize) {
		List<Device> deviceList = deviceMaintenanceMapper.getDeviceList(productType, productId, deviceId, currentPage, pageSize);
		List<Map<String,Object>> deviceMapList = new ArrayList<>();
		Map<String,Object> deviceListMap = new HashMap<>();
		if(deviceList.size()>0){
			for(Device device:deviceList){
				Map<String,Object> deviceMap = new LinkedHashMap<>();
				deviceMap.put("deviceId",device.getDeviceId());
				deviceMap.put("deviceName",device.getDeviceName());
				if(null!=device.getDeviceShadow()){
					deviceMap.put("deviceOnlineState",device.getDeviceShadow().getState().get("reported").get("online"));
				}
				deviceMapList.add(deviceMap);
			}
			deviceListMap.put("deviceList",deviceMapList);
			deviceListMap.put("deviceCount",deviceMaintenanceMapper.getCollectionCount(productType, productId,"device"));
		}
		return deviceListMap;
	}

	/**
	 *
	 * @param deviceId 设备id
	 * @param control 控制指令
	 * @return Integer
	 */
	@Override
	public Integer updateDevice(String deviceId, String control,String userId,String productId) {
		int controlState = 0;
		switch (control){
			case "ON":
				boolean openResult = this.controlDevice(deviceId,userId,control);
				if(!openResult){
					controlState=1;
				}
				break;
			case "OFF":
				//关机
				boolean offResult = this.controlDevice(deviceId,userId,control);
				if(!offResult){
					controlState=1;
				}
				break;
			case "resetDevice":
				//恢复默认配置

				break;
				default:
					try {
						throw new Exception("未知参数");
					} catch (Exception e) {
						e.printStackTrace();
					}
		}
		return controlState;
	}

	/**
	 * 查询设备维护记录
	 * @param  productType 所属产品
	 * @param productId 产品id
	 * @param deviceId 设备Id
	 * @param currentPage 当前页码
	 * @param pageSize 总页数。
	 * @return Object
	 */
	@Override
	public Object getDeviceRecordList(String productType, String productId, String deviceId, Integer currentPage, Integer pageSize) {
		List<DeviceMaintenanceRecordDTO> recordList = deviceMaintenanceMapper.getDeviceMaintenanceRecordList(productType, productId, deviceId, currentPage, pageSize);
		Map<String,Object> deviceListMap = new HashMap<>();
		deviceListMap.put("recordList",recordList);
		deviceListMap.put("count",deviceMaintenanceMapper.getCollectionCount(productType, productId,"device_maintenance_record"));
		return deviceListMap;
	}

	/**
	 *说明：获取产品类型列表
	 *@author lilongfei
	 *@since 2019/9/6 15:03
	 */
	@Override
	public List<ProductType> getProductTypeList() {
		return deviceMaintenanceMapper.getProductTypeList();
	}

	/**
	 *说明：根据产品类型查询产品
	 *@author lilongfei
	 *@since 2019/9/6 16:13
	 */
	@Override
	public List<Product> getProductList(String productType) {
		return deviceMaintenanceMapper.productType(productType);
	}

	/**
	 *说明：查询设备列表
	 *@author lilongfei
	 *@since 2019/9/6 16:36
	 */
	@Override
	public List<Device> getDeviceList(String productId) {
		return deviceMaintenanceMapper.getDeviceListByProductId(productId);
	}
}
